package com.excesys.exsecs.om.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.excesys.exsecs.admin.api.entity.SysUser;
import com.excesys.exsecs.common.security.service.ExsecsUser;
import com.excesys.exsecs.common.security.util.SecurityUtils;
import com.excesys.exsecs.om.constans.OmCommonConstants;
import com.excesys.exsecs.om.dto.PlanDto;
import com.excesys.exsecs.om.dto.SheetDto;
import com.excesys.exsecs.om.entity.*;
import com.excesys.exsecs.om.mapper.OmWEventMapper;
import com.excesys.exsecs.om.mapper.OmWOrderHistoryMapper;
import com.excesys.exsecs.om.mapper.OmWOrderMapper;
import com.excesys.exsecs.om.mapper.SequenceMapper;
import com.excesys.exsecs.om.service.OmICheckSheetService;
import com.excesys.exsecs.om.vo.MaintenancePlanVo;
import com.excesys.exsecs.om.vo.PlanVo;
import com.excesys.exsecs.om.vo.RepairPlanVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Title: 计划生成任务单
 * @author: weiyingjie
 * @date:2023年3月8日 下午7:43:30
 * @version V1.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class PlanToSheetUtils {

	private final OmICheckSheetService omICheckSheetService;

	private final SequenceMapper sequenceMapper;

	private final OrderNoUtils orderNoUtils;

	private final OmWOrderHistoryMapper omWOrderHistoryMapper;

	private final OmWOrderMapper omWOrderMapper;

	private final OmWEventMapper omWEventMapper;

	/**
	 *
	 * @Title: 计划生成任务单
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2023年3月8日 下午7:56:56
	 */
	public boolean planToSheet(PlanVo planVo) throws ParseException {

		String planType = planVo.getPlanType();
		if (planType.equals(OmCommonConstants.PLAN_TYPE_CYCLE)) {
			String cycleType = planVo.getCycleType();
			if (cycleType.equals(OmCommonConstants.CYCLE_TYPE_DAY)) {
				generatorTaskByDay(planVo);
			}
			else if (cycleType.equals(OmCommonConstants.CYCLE_TYPE_WEEK)) {
				generatorTaskByWeek(planVo);
			}
		}
		else if (planType.equals(OmCommonConstants.PLAN_TYPE_CUSTOM)) {

			generatorTaskByCustom(planVo);
		}
		return false;
	}

	/**
	 *
	 * @Title: 创建计划时是否需要生成当天的任务单
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2023年3月8日 下午7:56:56
	 */
	public boolean generatorTaskToday(PlanDto planDto) {

		boolean flag = true;

		String plantype = planDto.getPlanType();// 计划类型
		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日

		if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CYCLE)) {
			String planStartTime = planDto.getPlanStartTime();// "2023-02-28"
			if (StrUtil.equals(String.valueOf(planStartTime), today)) {
				List<OmICheckPlanTime> planTimeList = planDto.getRelList();
				for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
					String planTime = planStartTime + " " + omICheckPlanTime.getExcuteStartTime() + ":00";
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(DateUtils.getDateFromStringA(planTime));
					System.out.println("DateUtils.getCurrentDate()：" + DateUtils.getCurrentDate());
					System.out.println("calendar.getTime()：" + calendar.getTime());
//					if (calendar.getTime().before(DateUtils.getCurrentDate())) { // 还没到生成任务时间
						// 当前执行时间还没到先生成任务单
						OmICheckSheet omICheckSheet = new OmICheckSheet();
						BeanUtils.copyProperties(planDto, omICheckSheet);// 复制计划的属性到任务单
						omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
						omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
						omICheckSheet.setExcuteDate(today);// 执行日期当天时间
						omICheckSheet.setCheckName(planDto.getPlanName());
						List<OmICheckUserRel> userRelList = planDto.getUserRelList();
						SheetDto sheetDto = new SheetDto();
						BeanUtil.copyProperties(omICheckSheet, sheetDto);
						sheetDto.setUserRelList(userRelList);
						flag = omICheckSheetService.saveSheet(sheetDto);
//					}
				}
			}
		}
		else if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CUSTOM)) {
			String excuteDate = planDto.getExcuteDate();
			List<String> excuteDateList = StrUtil.split(excuteDate, ",");
			for (String excuteDateStr : excuteDateList) {
				if (StrUtil.equals(String.valueOf(excuteDateStr), today)) {
					List<OmICheckPlanTime> planTimeList = planDto.getRelList();
					for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
						String planTime = excuteDateStr + " " + omICheckPlanTime.getExcuteStartTime() + ":00";
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(DateUtils.getDateFromStringA(planTime));
//						if (calendar.getTime().before(DateUtils.getCurrentDate())) { // 还没到生成任务时间
							// 当前执行时间还没到生成任务单
							OmICheckSheet omICheckSheet = new OmICheckSheet();
							BeanUtils.copyProperties(planDto, omICheckSheet);// 复制计划的属性到任务单
							omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
							omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
							omICheckSheet.setExcuteDate(today);// 执行日期当天时间
							omICheckSheet.setCheckName(planDto.getPlanName());
							List<OmICheckUserRel> userRelList = planDto.getUserRelList();
							SheetDto sheetDto = new SheetDto();
							BeanUtil.copyProperties(omICheckSheet, sheetDto);
							sheetDto.setUserRelList(userRelList);
							flag = omICheckSheetService.saveSheet(sheetDto);
//						}
					}

				}
			}

		}

		return flag;
	}

	/**
	 *
	 * @Title: 每天
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2023年3月8日 下午7:56:56
	 */
	public boolean generatorTaskByDay(PlanVo planVo) throws ParseException {

		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日
		String planStartTime = planVo.getPlanStartTime();// "2023-02-28"

		DateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = simpleFormat.parse(planStartTime);// "2021-03-01"
		Date endDate = simpleFormat.parse(today);// "2021-07-08"
		// long startTime = startDate.getTime();
		// long endTime = endDate.getTime();
		int days = DateUtils.daysBetween(startDate, endDate);// 两个日期间隔天数

		Integer frequency = planVo.getFrequency();// 间隔天数

		if (days % frequency == 0) {
			List<OmICheckPlanTime> planTimeList = planVo.getRelList();
			// 可以整除
			for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
				// Date date = Date.from( planStartTime.atZone(
				// ZoneId.systemDefault()).toInstant());
				// String StartTime =
				// DateUtil.getFormatTimeString(date,DateUtil.DATE_FORMATE_STRING_B);
				// String planTime = planStartTime+"
				// "+omICheckPlanTime.getExcuteStartTime()+":00";
				// Calendar calendar = Calendar.getInstance();
				// calendar.setTime(DateUtil.getDateFromStringA(planTime));
				// if(DateUtil.getCurrentDate().before(calendar.getTime())){ // 还没到生成任务时间
				// 当前执行时间还没到生成任务单
				OmICheckSheet omICheckSheet = new OmICheckSheet();
				BeanUtils.copyProperties(planVo, omICheckSheet);// 复制计划的属性到任务单
				omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
				omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
				omICheckSheet.setExcuteDate(today);// 执行日期当天时间
				List<OmICheckUserRel> userRelList = planVo.getUserRelList();
				SheetDto sheetDto = new SheetDto();
				BeanUtil.copyProperties(omICheckSheet, sheetDto);
				sheetDto.setUserRelList(userRelList);
				omICheckSheetService.saveSheet(sheetDto);
				// }
			}
		}
		else {
			// 不能整除
			// 当前日期不需要生成巡检单
		}

		return true;
	}

	/**
	 *
	 * @Title: 每周
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2023年3月8日 下午7:56:56
	 */
	public boolean generatorTaskByWeek(PlanVo planVo) throws ParseException {

		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日
		String planStartTime = planVo.getPlanStartTime();// "2023-02-28"

		DateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = simpleFormat.parse(planStartTime);// "2021-03-01"
		Date endDate = simpleFormat.parse(today);// "2021-07-08"
		long startTime = startDate.getTime();
		long endTime = endDate.getTime();
		int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));

		Integer frequency = planVo.getFrequency() * 7;// 间隔天数

		if (days % frequency == 0) {
			List<OmICheckPlanTime> planTimeList = planVo.getRelList();
			// 可以整除
			for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
				// Date date = Date.from( planStartTime.atZone(
				// ZoneId.systemDefault()).toInstant());
				// String StartTime =
				// DateUtil.getFormatTimeString(date,DateUtil.DATE_FORMATE_STRING_B);
				// String planTime = planStartTime+"
				// "+omICheckPlanTime.getExcuteStartTime()+":00";
				// Calendar calendar = Calendar.getInstance();
				// calendar.setTime(DateUtil.getDateFromStringA(planTime));
				// if(DateUtil.getCurrentDate().before(calendar.getTime())){ // 还没到生成任务时间
				// 当前执行时间还没到生成任务单
				OmICheckSheet omICheckSheet = new OmICheckSheet();
				BeanUtils.copyProperties(planVo, omICheckSheet);// 复制计划的属性到任务单
				omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
				omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
				List<OmICheckUserRel> userRelList = planVo.getUserRelList();
				SheetDto sheetDto = new SheetDto();
				BeanUtil.copyProperties(omICheckSheet, sheetDto);
				sheetDto.setUserRelList(userRelList);
				omICheckSheetService.saveSheet(sheetDto);
				// }
			}
		}
		else {
			// 不能整除
			// 当前日期不需要生成巡检单
		}

		return true;
	}

	/**
	 *
	 * @Title: 定时任务自定义
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2023年3月8日 下午7:56:56
	 */
	public boolean generatorTaskByCustom(PlanVo planVo) {

		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日
		String excuteDate = planVo.getExcuteDate();
		List<String> excuteDateList = StrUtil.split(excuteDate, ",");
		for (String excuteDateStr : excuteDateList) {
			if (StrUtil.equals(String.valueOf(excuteDateStr), today)) {
				// Date planStartTime = DateUtil.getDateFromStringB(excuteDateStr);
				List<OmICheckPlanTime> planTimeList = planVo.getRelList();
				for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
					// String StartTime =
					// DateUtil.getFormatTimeString(planStartTime,DateUtil.DATE_FORMATE_STRING_B);
					String planTime = excuteDateStr + " " + omICheckPlanTime.getExcuteStartTime() + ":00";
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(DateUtils.getDateFromStringA(planTime));
					if (DateUtils.getCurrentDate().before(calendar.getTime())) { // 还没到生成任务时间
						// 当前执行时间还没到生成任务单
						OmICheckSheet omICheckSheet = new OmICheckSheet();
						BeanUtils.copyProperties(planVo, omICheckSheet);// 复制计划的属性到任务单
						omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
						omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
						List<OmICheckUserRel> userRelList = planVo.getUserRelList();
						SheetDto sheetDto = new SheetDto();
						BeanUtil.copyProperties(omICheckSheet, sheetDto);
						sheetDto.setUserRelList(userRelList);
						omICheckSheetService.saveSheet(sheetDto);
					}
				}

			}
		}
		return true;
	}

	/**
	 *
	 * @Title: 创建计划时是否需要生成当天的任务单
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2023年3月8日 下午7:56:56
	 */
	public boolean generatorRepairTaskToday(OmRepairPlan repairPlan) {

		boolean flag = true;

		String plantype = repairPlan.getPlanType();// 计划类型
		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日
		if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CYCLE)) {
			String planStartTime = repairPlan.getPlanStartTime();// "2023-02-28"
			if (StrUtil.equals(String.valueOf(planStartTime), today)) {
				// 0-工单/1-事件
				if ("0".equals(repairPlan.getPlanFlag())) {
					// 生成工单号
					int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
					OmWOrder omWOrder = new OmWOrder();
					omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
					omWOrder.setOrderName("维修计划-" + omWOrder.getOrderNo());
					omWOrder.setOrderFrom("5");
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
					omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
					omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建工单");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					// 工单报告人信息
					omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
					omWOrder.setOrderUserId(ExsecsUser.getId());
					omWOrder.setOrderUserName(ExsecsUser.getName());
					omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
					omWOrder.setRepairType("1");
					omWOrder.setEquipNo(repairPlan.getDeviceCode());
					omWOrder.setEquipName(repairPlan.getDeviceName());
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
					omWOrderHistory.setEventNo(omWOrder.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
					// 生成事件
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
					omWOrderHistorys.setOperation("创建事件");
					ExsecsUser ExsecsUsers = SecurityUtils.getUser();
					omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
					omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
					omWOrderHistorys.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistorys);
					omWEvent.setRepairType("1");
					omWEvent.setEventStatus("3");
					omWEvent.setEventUserId(ExsecsUsers.getId());
					omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
					omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
				else {
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建事件");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					omWEvent.setRepairType("1");
					omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
					omWEvent.setEventUserId(ExsecsUser.getId());
					omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
			}
		}
		else if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CUSTOM)) {
			String excuteDate = repairPlan.getPlanStartTime();
			if (StrUtil.equals(String.valueOf(excuteDate), today)) {
				// 0-工单/1-事件
				if ("0".equals(repairPlan.getPlanFlag())) {
					// 生成工单号
					int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
					OmWOrder omWOrder = new OmWOrder();
					omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
					omWOrder.setOrderName("维修计划-" + omWOrder.getOrderNo());
					omWOrder.setOrderFrom("5");
					omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
					omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建工单");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					// 工单报告人信息
					omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
					omWOrder.setOrderUserId(ExsecsUser.getId());
					omWOrder.setOrderUserName(ExsecsUser.getName());
					omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
					omWOrder.setRepairType("1");
					omWOrder.setEquipNo(repairPlan.getDeviceCode());
					omWOrder.setEquipName(repairPlan.getDeviceName());
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
					omWOrderHistory.setEventNo(omWOrder.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
					// 生成事件
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
					omWOrderHistorys.setOperation("创建事件");
					ExsecsUser ExsecsUsers = SecurityUtils.getUser();
					omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
					omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
					omWOrderHistorys.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistorys);
					omWEvent.setRepairType("1");
					omWEvent.setEventStatus("3");
					omWEvent.setEventUserId(ExsecsUsers.getId());
					omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
					omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
				else {
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建事件");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					omWEvent.setRepairType("1");
					omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
					omWEvent.setEventUserId(ExsecsUser.getId());
					omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
			}
		}

		return flag;
	}

	public Boolean executeRepairTaskToday(RepairPlanVo repairPlanVo) {
		boolean flag = true;
		String plantype = repairPlanVo.getPlanType();// 计划类型
		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日
		if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CYCLE)) {
			String planStartTime = repairPlanVo.getPlanStartTime().substring(0, 10);// "2023-02-28"
			if (StrUtil.equals(planStartTime, today)) {
				String planTime = repairPlanVo.getExcuteTime();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtils.getDateFromStringA(planTime));
				log.info("DateUtils.getCurrentDate()：" + DateUtils.getCurrentDate());
				log.info("calendar.getTime()：" + calendar.getTime());
				if (calendar.getTime().before(DateUtils.getCurrentDate())) { // 还没到生成任务时间
					// 0-工单/1-事件
					if ("0".equals(repairPlanVo.getPlanFlag())) {
						// 生成工单号
						int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
						OmWOrder omWOrder = new OmWOrder();
						omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
						omWOrder.setOrderName("维修计划-" + omWOrder.getOrderNo());
						omWOrder.setOrderFrom("5");
						DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
						omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
						omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建工单");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						// 工单报告人信息
						omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
						omWOrder.setOrderUserId(ExsecsUser.getId());
						omWOrder.setOrderUserName(ExsecsUser.getName());
						omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
						omWOrder.setRepairType("1");
						omWOrder.setEquipNo(repairPlanVo.getDeviceCode());
						omWOrder.setEquipName(repairPlanVo.getDeviceName());
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
						omWOrderHistory.setEventNo(omWOrder.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
						// 生成事件
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
						omWOrderHistorys.setOperation("创建事件");
						ExsecsUser ExsecsUsers = SecurityUtils.getUser();
						omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
						omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
						omWOrderHistorys.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistorys);
						omWEvent.setRepairType("1");
						omWEvent.setEventStatus("3");
						omWEvent.setEventUserId(ExsecsUsers.getId());
						omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
						omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
					else {
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建事件");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						omWEvent.setRepairType("1");
						omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
						omWEvent.setEventUserId(ExsecsUser.getId());
						omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
				}
			}
		}
		else if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CUSTOM)) {
			String excuteDate = repairPlanVo.getExcuteDate();
			if (StrUtil.equals(String.valueOf(excuteDate), today)) {
				String planTime = repairPlanVo.getExcuteTime();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtils.getDateFromStringA(planTime));
				if (calendar.getTime().before(DateUtils.getCurrentDate())) { // 还没到生成任务时间
					// 当前执行时间还没到生成任务单
					// 0-工单/1-事件
					if ("0".equals(repairPlanVo.getPlanFlag())) {
						// 生成工单号
						int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
						OmWOrder omWOrder = new OmWOrder();
						omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
						DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
						omWOrder.setOrderName("维修计划-" + omWOrder.getOrderNo());
						omWOrder.setOrderFrom("5");
						omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
						omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建工单");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						// 工单报告人信息
						omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
						omWOrder.setOrderUserId(ExsecsUser.getId());
						omWOrder.setOrderUserName(ExsecsUser.getName());
						omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
						omWOrder.setRepairType("1");
						omWOrder.setEquipNo(repairPlanVo.getDeviceCode());
						omWOrder.setEquipName(repairPlanVo.getDeviceName());
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
						omWOrderHistory.setEventNo(omWOrder.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
						// 生成事件
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
						omWOrderHistorys.setOperation("创建事件");
						ExsecsUser ExsecsUsers = SecurityUtils.getUser();
						omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
						omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
						omWOrderHistorys.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistorys);
						omWEvent.setRepairType("1");
						omWEvent.setEventStatus("3");
						omWEvent.setEventUserId(ExsecsUsers.getId());
						omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
						omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
					else {
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建事件");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						omWEvent.setRepairType("1");
						omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
						omWEvent.setEventUserId(ExsecsUser.getId());
						omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
				}
			}
		}

		return flag;
	}

	/**
	 *
	 * @Title: 创建计划时是否需要生成当天的任务单
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2023年3月8日 下午7:56:56
	 */
	public boolean generatorMintenanceTaskToday(OmMaintenancePlan omMintenancePlan) {

		boolean flag = true;

		String plantype = omMintenancePlan.getPlanType();// 计划类型
		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日
		if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CYCLE)) {
			String planStartTime = omMintenancePlan.getPlanStartTime();// "2023-02-28"
			if (StrUtil.equals(String.valueOf(planStartTime), today)) {
				// 0-工单/1-事件
				if ("0".equals(omMintenancePlan.getPlanFlag())) {
					// 生成工单号
					int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
					OmWOrder omWOrder = new OmWOrder();
					omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
					omWOrder.setOrderName("保养计划-" + omWOrder.getOrderNo());
					omWOrder.setOrderFrom("5");
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
					omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
					omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建工单");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					// 工单报告人信息
					omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
					omWOrder.setOrderUserId(ExsecsUser.getId());
					omWOrder.setOrderUserName(ExsecsUser.getName());
					omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
					omWOrder.setRepairType("2");
					omWOrder.setEquipNo(omMintenancePlan.getDeviceCode());
					omWOrder.setEquipName(omMintenancePlan.getDeviceName());
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
					omWOrderHistory.setEventNo(omWOrder.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
					// 生成事件
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
					omWOrderHistorys.setOperation("创建事件");
					ExsecsUser ExsecsUsers = SecurityUtils.getUser();
					omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
					omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
					omWOrderHistorys.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistorys);
					omWEvent.setRepairType("2");
					omWEvent.setEventStatus("3");
					omWEvent.setEventUserId(ExsecsUsers.getId());
					omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
					omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
				else {
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建事件");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					omWEvent.setRepairType("2");
					omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
					omWEvent.setEventUserId(ExsecsUser.getId());
					omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
			}
		}
		else if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CUSTOM)) {
			String excuteDate = omMintenancePlan.getPlanStartTime();
			if (StrUtil.equals(String.valueOf(excuteDate), today)) {
				// 0-工单/1-事件
				if ("0".equals(omMintenancePlan.getPlanFlag())) {
					// 生成工单号
					int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
					OmWOrder omWOrder = new OmWOrder();
					omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
					omWOrder.setOrderName("保养计划-" + omWOrder.getOrderNo());
					omWOrder.setOrderFrom("5");
					omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
					omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建工单");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					// 工单报告人信息
					omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
					omWOrder.setOrderUserId(ExsecsUser.getId());
					omWOrder.setOrderUserName(ExsecsUser.getName());
					omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
					omWOrder.setRepairType("2");
					omWOrder.setEquipNo(omMintenancePlan.getDeviceCode());
					omWOrder.setEquipName(omMintenancePlan.getDeviceName());
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
					omWOrderHistory.setEventNo(omWOrder.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
					// 生成事件
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
					omWOrderHistorys.setOperation("创建事件");
					ExsecsUser ExsecsUsers = SecurityUtils.getUser();
					omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
					omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
					omWOrderHistorys.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistorys);
					omWEvent.setRepairType("2");
					omWEvent.setEventStatus("3");
					omWEvent.setEventUserId(ExsecsUsers.getId());
					omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
					omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
				else {
					int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
					OmWEvent omWEvent = new OmWEvent();
					omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
					omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
					OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
					omWOrderHistory.setOperation("创建事件");
					ExsecsUser ExsecsUser = SecurityUtils.getUser();
					omWOrderHistory.setOperateUserId(ExsecsUser.getId());
					omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
					omWOrderHistory.setEventNo(omWEvent.getEventNo());
					omWOrderHistoryMapper.insert(omWOrderHistory);
					omWEvent.setRepairType("2");
					omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
					omWEvent.setEventUserId(ExsecsUser.getId());
					omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
					return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
				}
			}
		}

		return flag;
	}

	public boolean executeMintenanceTaskToday(MaintenancePlanVo maintenancePlanVo) {

		boolean flag = true;

		String plantype = maintenancePlanVo.getPlanType();// 计划类型
		String today = DateUtils.getNow(DateUtils.DATE_FORMATE_STRING_B);// 当前时间年月日
		if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CYCLE)) {
			String planStartTime = maintenancePlanVo.getPlanStartTime().substring(0, 10);// "2023-02-28"
			if (StrUtil.equals(planStartTime, today)) {
				String planTime = maintenancePlanVo.getExcuteDate();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtils.getDateFromStringA(planTime));
				log.info("DateUtils.getCurrentDate()：" + DateUtils.getCurrentDate());
				log.info("calendar.getTime()：" + calendar.getTime());
				if (calendar.getTime().before(DateUtils.getCurrentDate())) { // 还没到生成任务时间
					// 0-工单/1-事件
					if ("0".equals(maintenancePlanVo.getPlanFlag())) {
						// 生成工单号
						int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
						OmWOrder omWOrder = new OmWOrder();
						omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
						omWOrder.setOrderName("保养计划-" + omWOrder.getOrderNo());
						omWOrder.setOrderFrom("5");
						DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
						omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
						omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建工单");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						// 工单报告人信息
						omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
						omWOrder.setOrderUserId(ExsecsUser.getId());
						omWOrder.setOrderUserName(ExsecsUser.getName());
						omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
						omWOrder.setRepairType("2");
						omWOrder.setEquipNo(maintenancePlanVo.getDeviceCode());
						omWOrder.setEquipName(maintenancePlanVo.getDeviceName());
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
						omWOrderHistory.setEventNo(omWOrder.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
						// 生成事件
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
						omWOrderHistorys.setOperation("创建事件");
						ExsecsUser ExsecsUsers = SecurityUtils.getUser();
						omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
						omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
						omWOrderHistorys.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistorys);
						omWEvent.setRepairType("2");
						omWEvent.setEventStatus("3");
						omWEvent.setEventUserId(ExsecsUsers.getId());
						omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
						omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
					else {
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建事件");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						omWEvent.setRepairType("2");
						omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
						omWEvent.setEventUserId(ExsecsUser.getId());
						omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
				}
			}
		}
		else if (StrUtil.equals(plantype, OmCommonConstants.PLAN_TYPE_CUSTOM)) {
			String excuteDate = maintenancePlanVo.getExcuteDate();
			if (StrUtil.equals(String.valueOf(excuteDate), today)) {
				String planTime = maintenancePlanVo.getExcuteDate();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(DateUtils.getDateFromStringA(planTime));
				if (calendar.getTime().before(DateUtils.getCurrentDate())) { // 还没到生成任务时间
					// 当前执行时间还没到生成任务单
					// 0-工单/1-事件
					if ("0".equals(maintenancePlanVo.getPlanFlag())) {
						// 生成工单号
						int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
						OmWOrder omWOrder = new OmWOrder();
						omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
						DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
						omWOrder.setOrderName("保养计划-" + omWOrder.getOrderNo());
						omWOrder.setOrderFrom("5");
						omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
						omWOrder.setOrderStatus("3");// 待指派状态
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建工单");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						// 工单报告人信息
						omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
						omWOrder.setOrderUserId(ExsecsUser.getId());
						omWOrder.setOrderUserName(ExsecsUser.getName());
						omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
						omWOrder.setRepairType("2");
						omWOrder.setEquipNo(maintenancePlanVo.getDeviceCode());
						omWOrder.setEquipName(maintenancePlanVo.getDeviceName());
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
						omWOrderHistory.setEventNo(omWOrder.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
						// 生成事件
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
						omWOrderHistorys.setOperation("创建事件");
						ExsecsUser ExsecsUsers = SecurityUtils.getUser();
						omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
						omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
						omWOrderHistorys.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistorys);
						omWEvent.setRepairType("2");
						omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
						omWEvent.setEventUserId(ExsecsUsers.getId());
						omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
						omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
					else {
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("保养计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("创建事件");
						ExsecsUser ExsecsUser = SecurityUtils.getUser();
						omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						omWEvent.setRepairType("2");
						omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
						omWEvent.setEventUserId(ExsecsUser.getId());
						omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
						return SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
				}
			}
		}

		return flag;
	}

	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateUtils.getDateFromStringA("2023-08-17 09:30:00"));
		System.out.println("a：" + DateUtils.getCurrentDate());
		System.out.println("b：" + calendar.getTime());
		if (DateUtils.getCurrentDate().before(calendar.getTime())) {
			System.out.println("a比b大");
		}
		else if (calendar.getTime().before(DateUtils.getCurrentDate())) {
			System.out.println("b比a大");
		}
		String a = "2023-08-17 11:37:17.853";
		System.out.println(a.substring(0, 10));
		// System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	}

}
